<template>
  <div style="width: 400px; margin: 0 auto">
    <h1 style="margin: 0 auto">员工列表综合练习</h1>
    <el-form style="width: 400px">
      <el-form-item label="姓名">
        <el-input placeholder="请输入姓名" v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="工资">
        <el-input placeholder="请输入工资" v-model="user.salary"></el-input>
      </el-form-item>
      <el-form-item label="岗位">
        <el-input placeholder="请输入岗位" v-model="user.job"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="success" @click="submit" style="margin-right: 400px">提交表单</el-button>
  <el-table :data="userArr" style="width: 800px; ">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="username" label="姓名" align="center"></el-table-column>
      <el-table-column prop="salary" label="工资" align="center"></el-table-column>
      <el-table-column prop="job" label="岗位" align="center"></el-table-column>
      <el-table-column  label="操作">
        <template #default="scope">
          <el-button type="danger" @click="del(scope.row,scope.$index)">删除</el-button>
        </template>
      </el-table-column>
  </el-table>
  </div>


</template>

<script setup>
import {ref} from "vue";

const userArr = ref(
    []
);
const user = ref(
    {username: '', salary: '', job: ''}
);

const submit = () => {
  console.log(typeof user.value.salary)
  if (!user.value.salary||user.value.salary.trim() == '' ||
      !user.value.username||user.value.username.trim() == '' ||
      !user.value.job||user.value.job.trim() == '') {
    alert('不能输入空值');
    return;
  }
  if (isNaN(parseInt(user.value.salary))) {
    alert('工资需要为数字!');
    return;
  }
  userArr.value.push(user.value);
 user.value = {};
}
const del = (row, index)=>{
  if (confirm('确定删除'+row.username+'员工？'))
  userArr.value.splice(index,1);
}

</script>

<style scoped>

</style>